package com.healthcare;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;;

/**
 * Servlet implementation class RegistrationServlet
 */
public class Registration extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Registration() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
String address = null;
		
		if (request.getParameter("register") != null){
			address = "/Registered.jsp";
		}
		else if(request.getParameter("listSites") != null)
		{
			address = "/Register.jsp";	
		}
		
		RequestDispatcher dispatcher =
		request.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String inputfname = request.getParameter("name");
		String inputLastName = request.getParameter("lname");
		String inputEmail = request.getParameter("email");
		String inputAge = request.getParameter("age");
		String inputGender = request.getParameter("sex");
		String inputPassword = request.getParameter("password");
		//String code = request.getParameter("code");

		//User user = new User(inputfname, inputLastName, inputEmail, inputAge,
			//	inputGender, inputPassword);
		HealthCareServerProxy proxy = new HealthCareServerProxy();
		String name = proxy.registration(inputfname, inputLastName,inputEmail, inputAge,inputGender, inputPassword);
		if(name != null)
		{
			request.setAttribute("name", name);
			
			RequestDispatcher dispatcher = request.getRequestDispatcher("/Welcome.jsp");
			dispatcher.forward(request, response);
			
			
		} else {
			RequestDispatcher dispatcher = request.getRequestDispatcher("/Home.jsp");
			request.setAttribute("error", true);
			dispatcher.forward(request, response);
		}
		return;	
	}

}
